package com.tanhua.back.service.impl;

import com.tanhua.back.service.FreezeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author maorf
 * @version 1.0
 */
@Service
public class FreezeServiceImpl implements FreezeService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //冻结用户
    @Override
    public void freeze(Map map) {
        Integer userId = (Integer) map.get("userId");
        String freezingTime = (String) map.get("freezingTime");
        String freezingRange = (String) map.get("freezingRange");
        int day=0;
        if (freezingTime.equals("1")){
            day=3;
        }else if (freezingTime.equals("3")){
            day=7;
        }else{
            day=-1;
        }
        redisTemplate.opsForHash().put("freeze_"+userId,"freezingRange",freezingRange.toString());
        redisTemplate.opsForHash().put("freeze_"+userId,"reasonsForFreezing",map.get("reasonsForFreezing").toString());
        redisTemplate.expire("freeze_"+userId,day,TimeUnit.DAYS);
    }

    //解冻用户
    @Override
    public void unfreeze(Map map) {
        redisTemplate.delete("freeze_"+map.get("userId"));
    }
}
